﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class DrawFunction3 : MonoBehaviour {
	public static DrawFunction3 Instance;

	public GameObject cubePrefab;
	public float interval;
	public float rangeX;
	public float rangeY;
	public float zAmplificationRatio;
	public float redRatio;
	public float greenRatio;
	public float blueRatio;

	float x;
	float y;
	float z;

	List<Transform> cubePosList = new List<Transform>();
	List<Vector3> cubePosRecord = new List<Vector3>();

	private void Awake() {
		Instance = this;
	}

	void Start() {
		DrawFunction();
	}

	private void Update() {
		for (int i = 0; i < cubePosList.Count; i++) {
			//cubePosList[i].position.y = cubePosRecord[i].y * zAmplificationRatio;
			cubePosList[i].position = new Vector3(cubePosList[i].position.x, cubePosRecord[i].y * zAmplificationRatio, cubePosList[i].position.z);
			//cubePosList[i].gameObject.GetComponent<MeshRenderer>().material.color = PostionToColor(cubePosList[i].position);
		}
	}

	void DrawFunction() {
		for (int i = (int)(-rangeX / interval); i * interval < rangeX; i++) {
			for (int j = (int)(-rangeY / interval); j * interval < rangeY; j++) {
				x = i * interval;
				y = j * interval;
				z = 1.0f / (x * x + y * y + 2);
				AddCube(new Vector3(x, z, y));
			}
		}
	}

	void AddCube(Vector3 vector3) {
		GameObject go = Instantiate(cubePrefab, vector3, Quaternion.identity);
		cubePosList.Add(go.transform);
		cubePosRecord.Add(go.transform.position);
	}

	Color PostionToColor(Vector3 vector3) {
		return new Color(vector3.y * redRatio, vector3.y * greenRatio, vector3.y * blueRatio);
	}

}
